package com.ruoyi.record.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.common.RecordConstant;
import com.ruoyi.record.domain.WordGroupList;
import com.ruoyi.record.domain.WordStudyStepProgress;
import com.ruoyi.record.service.IWordStudyStepProgressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.WordTestRecordMapper;
import com.ruoyi.record.domain.WordTestRecord;
import com.ruoyi.record.service.IWordTestRecordService;

/**
 * 单词测试记录Service业务层处理
 *
 * @author ruoyi
 * 2025-07-02
 */
@Slf4j
@Service
public class WordTestRecordServiceImpl implements IWordTestRecordService
{
    @Autowired
    private WordTestRecordMapper wordTestRecordMapper;

    @Autowired
    private IWordStudyStepProgressService progressService;

    @Autowired
    private WordRecordCommonService wordRecordCommonService;

    /**
     * 查询单词测试记录
     *
     * @param id 单词测试记录主键
     * @return 单词测试记录
     */
    @Override
    public WordTestRecord selectWordTestRecordById(Integer id)
    {
        return wordTestRecordMapper.selectWordTestRecordById(id);
    }

    /**
     * 查询单词测试记录列表
     *
     * @param wordTestRecord 单词测试记录
     * @return 单词测试记录
     */
    @Override
    public List<WordTestRecord> selectWordTestRecordList(WordTestRecord wordTestRecord)
    {
        return wordTestRecordMapper.selectWordTestRecordList(wordTestRecord);
    }

    @Override
    public List<WordTestRecord> selectWordTestRecordListByUser(WordTestRecord wordTestRecord)
    {
        return wordTestRecordMapper.selectWordTestRecordListByUser(wordTestRecord);
    }

    /**
     * 按日期分组
     * */
    @Override
    public List<WordGroupList> selectRecordGroupList(){
        return wordTestRecordMapper.selectRecordGroupList();
    }

    @Override
    public List<WordGroupList> selectCountGroupList(){
        return wordTestRecordMapper.selectCountGroupList();
    }

    @Override
    public List<WordGroupList> selectSchoolGroupList(WordGroupList wordGroupList){
        return wordTestRecordMapper.selectSchoolGroupList(wordGroupList);
    }

    @Override
    public List<WordGroupList> selectSchoolStudentGroupList(WordGroupList wordGroupList){
        return wordTestRecordMapper.selectSchoolStudentGroupList(wordGroupList);
    }

    @Override
    public List<WordGroupList> selectSchoolWeekGroupList(){
        return wordTestRecordMapper.selectSchoolWeekGroupList();
    }

    @Override
    public List<WordGroupList> selectSchoolStudentWeekGroupList(){
        return wordTestRecordMapper.selectSchoolStudentWeekGroupList();
    }

    @Override
    public List<WordGroupList> selectSchoolMonthGroupList(){
        return wordTestRecordMapper.selectSchoolMonthGroupList();
    }

    @Override
    public List<WordGroupList> selectSchoolMonthStudentGroupList(){
        return wordTestRecordMapper.selectSchoolMonthStudentGroupList();
    }

    /**
     * 新增单词测试记录
     *
     * @param wordTestRecord 单词测试记录
     * @return 结果
     */
    @Override
    public int insertWordTestRecord(WordTestRecord wordTestRecord)
    {
        WordTestRecord record = wordRecordCommonService.insertList(wordTestRecord);
        record.setCreateTime(DateUtils.getNowDate());

        int inserted = wordTestRecordMapper.insertWordTestRecord(record);

        //处理是否有已分配-未完成任务
        JSONObject jsonObject = new JSONObject();
        if (record.getBook() != null && record.getUnit() != null) {
            jsonObject.put("book", record.getBook());
            jsonObject.put("unit", record.getUnit());
        }
        else {
            jsonObject.put("group", record.getGroup());
        }

        jsonObject.put("rate",record.getCorrectRate());
        jsonObject.put("type", RecordConstant.TASK_TYPE_WORD_TEST);
        Long userId = record.getUserId();
        wordRecordCommonService.handleProcess(jsonObject, userId);

        return inserted;
    }

    /**
     * 修改单词测试记录
     *
     * @param wordTestRecord 单词测试记录
     * @return 结果
     */
    @Override
    public int updateWordTestRecord(WordTestRecord wordTestRecord)
    {
        wordTestRecord.setUpdateTime(DateUtils.getNowDate());
        return wordTestRecordMapper.updateWordTestRecord(wordTestRecord);
    }

    /**
     * 批量删除单词测试记录
     *
     * @param ids 需要删除的单词测试记录主键
     * @return 结果
     */
    @Override
    public int deleteWordTestRecordByIds(Integer[] ids)
    {
        return wordTestRecordMapper.deleteWordTestRecordByIds(ids);
    }

    /**
     * 删除单词测试记录信息
     *
     * @param id 单词测试记录主键
     * @return 结果
     */
    @Override
    public int deleteWordTestRecordById(Integer id)
    {
        return wordTestRecordMapper.deleteWordTestRecordById(id);
    }

    @Override
    public List<WordTestRecord> selectSeniorRecordList(WordTestRecord wordTestRecord) {
        return wordTestRecordMapper.selectSeniorRecordList(wordTestRecord);
    }
}
